﻿/**
 *  Track Network Map 2.0, NetControl Project 
 *  by Jie Gao, on Dec, 2009.
 * 
 *  Add New Bookmark Window
 * 
 */

import mx.rpc.http.HTTPService;
import mx.rpc.events.ResultEvent;

import mx.managers.PopUpManager;
import mx.collections. *;
import mx.controls. *;
import com.esri.ags.Map;
import com.esri.ags.geometry.Extent;
import com.uprr.app.tnm.model.Model;
import com.uprr.app.tnm.events.AppEvent;

import com.adobe.serialization.json.JSON

private function handleCreationComplete () : void 
{
	// Center the TitleWindow container
	// over the control that created it.
	PopUpManager.centerPopUp (this);
}


// Event handler for the OK button.
private function addNewBkmk () : void
{
	try 
	{
		var ex : Extent = Model.getInstance().map.extent;
		
		/*
		var bkmkId : int = 0;
		Model.getInstance().bkmkList.addItem ( 
		{
			bkmkDesc : bkmkTitle.text, bkmkId : bkmkId, minX : ex.xmin, minY : ex.ymin, maxX : ex.xmax, maxY : ex.ymax
			//bkmkDesc : "something", bkmkId : bkmkId, minX : 0, minY : 0, maxX : 100, maxY : 100
			
		});
		*/
		
		var httpRequest:HTTPService = new HTTPService();
		var rnd : Number = Math.round(Math.random()*1000);
		httpRequest.url = "MyBookmarksServlet?x=" + rnd;
		
		var parameters:Object=new Object();
		parameters.action="add";
		parameters.bkmkdesc = bkmkTitle.text;
		parameters.svcFolder=Model.getInstance ().overlayMapServiceFolder;
		parameters.svcName=Model.getInstance ().overlayMapServiceName;
		parameters.minx=ex.xmin;
		parameters.miny=ex.ymin;
		parameters.maxx=ex.xmax;
		parameters.maxy=ex.ymax;
		
		httpRequest.request=parameters;
		httpRequest.resultFormat="text";
		httpRequest.addEventListener(ResultEvent.RESULT, addNewBkmkResultHandler);
		httpRequest.addEventListener("fault", serviceFaultHandler);
		httpRequest.send();
		
		PopUpManager.removePopUp (this);
	} catch (error : Error)
	{
		var errorEvent : AppEvent = new AppEvent (AppEvent.APP_ERROR);
		errorEvent.data = "Error when adding the bookmark: " + error.message;
		errorEvent.dispatch ();
	}
}

/**
 * add the returned bkmk to the datagrid, server sode code shall figure out the bkmkid
 * 
 * @usage   
 * @return  
 */

private function addNewBkmkResultHandler(event : ResultEvent):void {
	try 
	{
		var rawData : String = String (event.result);
		
		var arr : Array = (JSON.decode (rawData) as Array);
		for (var i : int = 0; i < arr.length; i ++)
		{
			Model.getInstance ().bkmkList.addItem ( 
			{
				bkmkDesc : arr [i].bkmkDesc, mapService:arr [i].mapService, bkmkId : arr [i].bkmkId, minX : arr [i].minX, minY : arr [i].minY, maxX : arr [i].maxX, maxY : arr [i].maxY
			} )
		}
	} catch (err : Error)
	{
		var errorEvent : AppEvent = new AppEvent (AppEvent.APP_ERROR);
		errorEvent.data = "Error when adding bookmarks,"+err.message;
		errorEvent.dispatch ();
	}
	 
}

// when something goes wrong for all HTTP services
private function serviceFaultHandler (event : Event) : void
{
	var errorEvent : AppEvent = new AppEvent (AppEvent.APP_ERROR);
		errorEvent.data = "Error when adding bookmarks.";
		errorEvent.dispatch ();
}
